package cn.com.api.push;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import cn.com.api.resultpage.InitPushMessagePage;
import cn.jpush.api.JPushClient;
import cn.jpush.api.common.ClientConfig;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class PushExample {
	protected static final Logger LOG = LogManager.getLogger(PushExample.class.getName());

	// demo App defined in resources/jpush-api.conf
	private static final String appKey = "1b2ec4fba2cbd8b1bd0ed1f0";
	private static final String masterSecret = "b4cb7a7ceba85e10e0d83a91";

	public static final String TITLE = "Test from API example";
	public static final String ALERT = "Test from API Example - alert";
	public static final String MSG_CONTENT = "Test from API Example - msgContent";
	public static final String REGISTRATION_ID = "040288953eb";
	public static final String TAG = "tag_api";

	/**
	 * 设备id 040288953eb 我的手机---用户端 李官玉 070915a22a7
	 */
	public static void main(String[] args) {

		double f = 3.1556;
		BigDecimal b = new BigDecimal(f);
		double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		System.out.println(f1);
		// // testSendPushWithCustomConfig();
		// //testSendPush();
		//
		// List<String> registrationIds = new ArrayList<String>();
		// registrationIds.add("040288953eb");
		// // registrationIds.add("070915a22a7");
		//
		// Map<String, String> map = new HashMap<String, String>();
		// map.put("test1", "test-javatestuser");
		// map.put("type", "1");
		//
		// try {
		// // IOSToSingleDevice.getInstance().iostosingledevice(
		// // "1b2ec4fba2cbd8b1bd0ed1f0", "b4cb7a7ceba85e10e0d83a91",
		// // "javatest-notice", map, registrationIds);
		// AndroidToSingleDevice.getInstance().androidtosingleDevice("1b2ec4fba2cbd8b1bd0ed1f0",
		// "b4cb7a7ceba85e10e0d83a91", "alert-message", "title-message", map,
		// registrationIds);
		// testSendAndroidMessage();
		// } catch (APIConnectionException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// } catch (APIRequestException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
	}

	public static void testShopPush() {
		// testSendPushWithCustomConfig();
		// testSendPush();

		List<String> registrationIds = new ArrayList<String>();
		registrationIds.add("070f6bd22b3");

		Map<String, String> map = new HashMap<String, String>();
		map.put("test1", "test-javatestshop");
		map.put("type", "1");

		try {
			// IOSToSingleDevice.getInstance().iostosingledevice(
			// "1b2ec4fba2cbd8b1bd0ed1f0", "b4cb7a7ceba85e10e0d83a91",
			// "javatest-notice", map, registrationIds);
			AndroidToSingleDevice.getInstance().androidtosingleDevice("5d90b1db84e72f56d349bb5b",
					"9b33efcd4bef8a57d7281fc6 ", "alert-message", "title-message", map, registrationIds);
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void testSendPush() {
		// HttpProxy proxy = new HttpProxy("localhost", 3128);
		// Can use this https proxy: https://github.com/Exa-Networks/exaproxy
		JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);

		// For push, all you need do is to build PushPayload object.
		PushPayload payload = buildPushObject_all_all_alert();

		try {
			PushResult result = jpushClient.sendPush(payload);
			LOG.info("Got result - " + result);

		} catch (APIConnectionException e) {
			LOG.error("Connection error. Should retry later. ", e);

		} catch (APIRequestException e) {
			LOG.error("Error response from JPush server. Should review and fix it. ", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Code: " + e.getErrorCode());
			LOG.info("Error Message: " + e.getErrorMessage());
			LOG.info("Msg ID: " + e.getMsgId());
		}
	}

	public static PushPayload buildPushObject_all_all_alert() {
		return PushPayload.alertAll(ALERT);
	}

	public static PushPayload buildPushObject_all_alias_alert() {
		return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias("alias1"))
				.setNotification(Notification.alert(ALERT)).build();
	}

	public static PushPayload buildPushObject_android_tag_alertWithTitle() {
		return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.tag("tag1"))
				.setNotification(Notification.android(ALERT, TITLE, null)).build();
	}

	public static PushPayload buildPushObject_android_and_ios() {
		return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.tag("tag1"))
				.setNotification(Notification.newBuilder().setAlert("alert content")
						.addPlatformNotification(AndroidNotification.newBuilder().setTitle("Android Title").build())
						.addPlatformNotification(
								IosNotification.newBuilder().incrBadge(1).addExtra("extra_key", "extra_value").build())
						.build())
				.build();
	}

	public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
		return PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.tag_and("tag1", "tag_all"))
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(IosNotification.newBuilder().setAlert(ALERT).setBadge(5)
								.setSound("happy").addExtra("from", "JPush").build())
						.build())
				.setMessage(Message.content(MSG_CONTENT))
				.setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
	}

	public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
		Map<String, String> map = new HashMap<String, String>();

		return PushPayload.newBuilder().setPlatform(Platform.android_ios())
				.setAudience(Audience.newBuilder().addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
						.addAudienceTarget(AudienceTarget.alias("alias1", "alias2")).build())
				.setMessage(Message.newBuilder().setMsgContent(MSG_CONTENT).addExtra("from", "JPush").build()).build();
	}

	public static void testSendPushWithCustomConfig() {
		ClientConfig config = ClientConfig.getInstance();
		// Setup the custom hostname
		config.setPushHostName("https://api.jpush.cn");

		JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3, null, config);

		// For push, all you need do is to build PushPayload object.
		PushPayload payload = buildPushObject_all_all_alert();

		try {
			PushResult result = jpushClient.sendPush(payload);
			LOG.info("Got result - " + result);

		} catch (APIConnectionException e) {
			LOG.error("Connection error. Should retry later. ", e);

		} catch (APIRequestException e) {
			LOG.error("Error response from JPush server. Should review and fix it. ", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Code: " + e.getErrorCode());
			LOG.info("Error Message: " + e.getErrorMessage());
			LOG.info("Msg ID: " + e.getMsgId());
		}
	}

	public static void testSendIosAlert() {
		JPushClient jpushClient = new JPushClient(masterSecret, appKey);

		IosAlert alert = IosAlert.newBuilder().setTitleAndBody("test alert", "test ios alert json")
				.setActionLocKey("PLAY").build();
		try {
			PushResult result = jpushClient.sendIosNotificationWithAlias(alert, new HashMap<String, String>(),
					"alias1");
			LOG.info("Got result - " + result);
		} catch (APIConnectionException e) {
			LOG.error("Connection error. Should retry later. ", e);
		} catch (APIRequestException e) {
			LOG.error("Error response from JPush server. Should review and fix it. ", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Code: " + e.getErrorCode());
			LOG.info("Error Message: " + e.getErrorMessage());
		}
	}

	public static void testSendAndroidMessage() {
		String appkey = "77e8dc86f347e2d255cfbc3d";
		String secretkey = "d0e7dbf58a1f49f7e0c40be0";
		String deviceId = "060df47eeba";
		List<String> registrationIds = new ArrayList<String>();
		registrationIds.add(deviceId);
		String alert = "您好，有新的信息-syy！";
		String title = "新消息!";
		Map<String, String> map = new HashMap<String, String>();

		JPushClient jpushClient = new JPushClient(secretkey, appkey, 3);

		InitPushMessagePage pushMessage = new InitPushMessagePage();
		pushMessage.setAlert_body("您好，有新的信息-syy！");

		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android_ios())
				.setAudience(Audience.registrationId(registrationIds))
				.setMessage(Message.newBuilder().setMsgContent(MSG_CONTENT).addExtra("from", "JPush").build()).build();

		try {
			PushResult result = jpushClient.sendPush(payload);
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
